#/bin/bash
# OS:centos7, user: root, auth: liliwang90
# 安装Nginx并反代updates.jenkins-ci.org
# 修改Hosts，将目标为updates.jenkins-ci.org的请求转向mirrors.tuna.tsinghua.edu.cn
# http://updates.jenkins-ci.org/download/plugins/jdk-tool/1.4/jdk-tool.hpi
# http://updates.jenkins-ci.org/download/plugins/docker-build-step/2.4/docker-build-step.hpi
# http://mirrors.tuna.tsinghua.edu.cn/jenkins/
sudo yum install yum-utils
sudo tee /etc/yum.repos.d/nginx.repo << EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install -y nginx
vi /etc/nginx/conf.d/default.conf
server {
        server_name     updates.jenkins-ci.org;
        listen          80;
        location / {
                return https://mirrors.tuna.tsinghua.edu.cn/jenkins/;
        }
        location /download {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_set_header Host mirrors.tuna.tsinghua.edu.cn;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
            rewrite /download(.*) /jenkins$1 break;
            proxy_pass https://mirrors.tuna.tsinghua.edu.cn;
        }
}

sudo echo '127.0.0.1 updates.jenkins-ci.org' >> /etc/hosts
cat /etc/hosts